home *** CD-ROM | disk | FTP | other *** search
/ Delphi Magazine Collection 2001 / Delphi Magazine Collection 20001 (2001).iso / DISKS / Issue36 / construc / DRBOBCGI.PAS next >
Encoding:
Pascal/Delphi Source File  |  1998-06-30  |  2.4 KB  |  116 lines

  1. unit DrBobCGI;
  2. {$I-}
  3. interface
  4. type
  5.   TCGIprotocol = (CGI,WinCGI);
  6. var
  7.   CGIProtocol: TCGIprotocol = CGI;
  8.  
  9. type
  10.   TRequestMethod = (Unknown,Get,Post);
  11. var
  12.   RequestMethod: TRequestMethod = Unknown;
  13.  
  14. var
  15.   ContentLength: Integer = 0;
  16.   Data: AnsiString = '';
  17.  
  18.   function Value(const Field: ShortString): ShortString;
  19.  
  20.   function ValueAsInteger(const Field: ShortString): Integer;
  21.  
  22. implementation
  23. uses
  24.   Windows, SysUtils;
  25.  
  26.   function Value(const Field: ShortString): ShortString;
  27.   var
  28.     i: Integer;
  29.     len: Byte absolute Result;
  30.   begin
  31.     Len := 0;
  32.     i := Pos('&'+Field+'=',Data);
  33.     if i = 0 then
  34.     begin
  35.       i := Pos(Field+'=',Data);
  36.       if i > 1 then i := 0
  37.     end
  38.     else Inc(i); { skip '&' }
  39.     if i > 0 then
  40.     begin
  41.       Inc(i,Length(Field)+1);
  42.       while Data[i] <> '&' do
  43.       begin
  44.         Inc(Len);
  45.         Result[Len] := Data[i];
  46.         Inc(i)
  47.       end
  48.     end
  49.   end {Value};
  50.  
  51.   function ValueAsInteger(const Field: ShortString): Integer;
  52.   begin
  53.     try
  54.       Result := StrToInt(Value(Field))
  55.     except
  56.       Result := 0
  57.     end
  58.   end {ValueAsInteger};
  59.  
  60. var
  61.   P: PChar;
  62.   i: Integer;
  63.   Str: ShortString;
  64.  
  65. initialization
  66.   P := GetEnvironmentStrings;
  67.   while P^ <> #0 do
  68.   begin
  69.     Str := StrPas(P);
  70.     if Pos('REQUEST_METHOD=',Str) > 0 then
  71.     begin
  72.       Delete(Str,1,Pos('=',Str));
  73.       if Str = 'POST' then RequestMethod := Post
  74.       else
  75.         if Str = 'GET' then RequestMethod := Get
  76.     end;
  77.     if Pos('CONTENT_LENGTH=',Str) = 1 then
  78.     begin
  79.       Delete(Str,1,Pos('=',Str));
  80.       ContentLength := StrToInt(Str)
  81.     end;
  82.     if Pos('QUERY_STRING=',Str) > 0 then
  83.     begin
  84.       Delete(Str,1,Pos('=',Str));
  85.       SetLength(Data,Length(Str)+1);
  86.       Data := Str
  87.     end;
  88.     Inc(P, StrLen(P)+1)
  89.   end;
  90.   if RequestMethod = Post then
  91.   begin
  92.     SetLength(Data,ContentLength+1);
  93.     for i:=1 to ContentLength do read(Data[i]);
  94.     Data[ContentLength+1] := '&';
  95.   { if IOResult <> 0 then { skip }
  96.   end;
  97.   i := 0;
  98.   while i < Length(Data) do
  99.   begin
  100.     Inc(i);
  101.     if Data[i] = '+' then Data[i] := ' ';
  102.     if Data[i] = '%' then { special code }
  103.     begin
  104.       Str := '$00';
  105.       Str[2] := Data[i+1];
  106.       Str[3] := Data[i+2];
  107.       Delete(Data,i+1,2);
  108.       Data[i] := Chr(StrToInt(Str))
  109.     end
  110.   end;
  111.   if i > 0 then Data[i+1] := '&'
  112.            else Data := '&';
  113. finalization
  114.   Data := ''
  115. end.
  116.